package conf

import (
	"flag"
	"fmt"
	"github.com/spf13/viper"
	// "gitee.com/zhanglibin/xlog/log"
)

type Conf struct {
	App AppInfo `mapstructure:"app"`

	Mysql MysqlInfo `mapstructure:"mysql"`

	// Xlog *log.Config `mapstructure:"xlog"`

}

type MysqlInfo struct {
	Host     string `mapstructure:"host"`
	Database string `mapstructure:"database"`
	Port     int    `mapstructure:"port"`
	User     string `mapstructure:"user"`
	Pass     string `mapstructure:"pass"`
	Timeout  string `mapstructure:"timeout`
}

type AppInfo struct {
	Name string `mapstructure:"name"`
	Port int    `mapstructure:"port"`
}

var (
	C           Conf
	Env         string
	CheckConfig bool
)

func init() {
	flag.StringVar(&Env, "env", "dev", "please set env")
	flag.BoolVar(&CheckConfig, "checkconfig", false, "please set test checkconfig")
	flag.Parse()
	loadConfig(Env)

	// C.Xlog.InitLogger()
}

func loadConfig(env string) {

	configFile := fmt.Sprintf("./conf/%s.conf.yaml", env)

	viper.SetConfigType("yaml")
	viper.SetConfigFile(configFile)

	err := viper.ReadInConfig()
	if err != nil {
		fmt.Errorf("read_config_failed err:%v", err)
		panic(err)
	}

	err = viper.Unmarshal(&C)

	fmt.Println(C)
	if err != nil {
		fmt.Errorf("unmarshal_config_failed err:%v", err)
		panic(err)
	}
}
